
//
// 110330 - AH - Created.
//

package org.soekrat.gui.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Locale;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

/**
 * <p>
 *   Widget to allow end user to select default 
 *   {@link Locale} before launch.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class LocalSelector extends JPanel 
{
  private final static class Renderer implements ListCellRenderer
  {
    private final DefaultListCellRenderer m_dlc=new DefaultListCellRenderer();

    public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) 
    {
      String text=((Locale)value).getDisplayName();
      return m_dlc.getListCellRendererComponent(list, text, index, isSelected, 
        cellHasFocus);
    }
    
  }
  
  private final JComboBox m_cb;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public LocalSelector()
  {
    setLayout(new FlowLayout(FlowLayout.LEFT));
    
    add(new JLabel("Language: "));
    
    m_cb=new JComboBox();
    m_cb.setRenderer(new Renderer());
    
    Locale[] locals=Locale.getAvailableLocales();
    for (Locale locale : locals) m_cb.addItem(locale);
    
    Locale l=Locale.getDefault();
   
    m_cb.setSelectedItem(l);
    
    m_cb.addItemListener
    (
      new ItemListener() 
      {
        public void itemStateChanged(ItemEvent e) 
        {
          Locale locale=(Locale)m_cb.getSelectedItem();
          if (locale!=null)
          {
            Locale.setDefault(locale);
          }
        }
      }
    );
    
    add(m_cb);
  }
  
  public static void main(String[] args)  
  {
    JFrame frame=new JFrame();
    Container c=frame.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(new LocalSelector(), BorderLayout.CENTER);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
  
}




